﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Data;
using System.Linq;
using Think9.Models;
using Think9.Services.Base;
using Think9.Services.Basic;

namespace Think9.Controllers.Basic
{
    [Area("SysBasic")]
    public class ModuleController : BaseController
    {
        private ModuleService ModuleService = new ModuleService();
        private ComService ComService = new ComService();

        [HttpGet]
        public override ActionResult Index(int? id)
        {
            base.Index(id);
            return View();
        }

        //左侧菜单 启动时调用
        [HttpGet]
        public JsonResult GetLeftTree()
        {
            object result = ModuleService.GetModuleList(CurrentUser.RoleId);
            return Json(result);
        }

        [HttpGet]
        public JsonResult GetList()
        {
            var list = ModuleService.GetAll();
            var result = new { code = 0, count = list.Count(), data = list };
            return Json(result);
        }

        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.SelectList = ModuleService.GetSelectTreeList();
            return View();
        }

        [HttpPost]
        public ActionResult Add(ModuleEntity model)
        {
            model.FontFamily = "";
            model.UpdateTime = DateTime.Now;
            model.Icon = model.Icon == null ? "" : model.Icon;
            model.UrlAddress = model.UrlAddress == null ? "" : model.UrlAddress;

            var result = ModuleService.Insert(model) ? SuccessTip("添加成功") : ErrorTip("添加失败");
            return Json(result);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            if (Think9.Services.Base.Configs.GetValue("IsDemo") == "true")
            {
                return Json("演示模式下不能编辑菜单！");
            }
            else
            {
                ViewBag.SelectList = ModuleService.GetSelectTreeList();

                var model = ModuleService.GetById(id);
                if (model != null)
                {
                    ViewBag.PId = model.ParentId;
                    return View(model);
                }
                else
                {
                    return Json("数据不存在！");
                }
            }
        }

        [HttpPost]
        public ActionResult Edit(ModuleEntity model)
        {
            model.UpdateTime = DateTime.Now;
            model.Icon = model.Icon == null ? "" : model.Icon;
            model.UrlAddress = model.UrlAddress == null ? "" : model.UrlAddress;

            string err = this.CheckParent(model.ParentId.ToString(), model.Id.ToString());
            if (err == "")
            {
                err = ModuleService.UpdateById(model) ? "" : "编辑失败";
            }

            if (err == "")
            {
                return Json(SuccessTip("编辑成功"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        [HttpGet]
        public JsonResult Delete(int id)
        {
            if (Think9.Services.Base.Configs.GetValue("IsDemo") == "true")
            {
                return Json(ErrorTip("演示模式下不能删除菜单！")); 
            }
            else
            {
                string err = "";
                if (ComService.GetTotal("sys_module", "where ParentId=" + id + " ") > 0)
                {
                    err = "不能删除，还有下级菜单";
                }
                else
                {
                    err = ModuleService.DeleteById(id) ? "" : "删除失败";
                    if (err == "")
                    {
                        ComService.ExecuteSql("delete from sys_roleauthorize where ModuleId='" + id + "'");
                    }
                }

                if (err == "")
                {
                    return Json(SuccessTip("删除成功"));
                }
                else
                {
                    return Json(ErrorTip(err));
                }
            }
        }

        [HttpGet]
        public JsonResult ModuleButtonList(int roleId)
        {
            var list = ModuleService.GetModuleButtonList(roleId);
            var result = new { code = 0, count = list.Count(), data = list };
            return Json(result);
        }

        public ActionResult Icon()
        {
            return View();
        }

        private string CheckParent(string selectParentID, string id)
        {
            string strErr = "";

            if (selectParentID == id)
            {
                strErr = "不能选择自己作为上级！";
            }
            else
            {
                DataTable dtAll = ComService.GetDataTable("select id,FullName as name,ParentId from sys_module order by OrderNo");

                string strAllUpDep = ModuleService.GetUpIDStr(dtAll, selectParentID);

                if (strAllUpDep.Contains("." + id + "."))
                {
                    strErr = "上级菜单选择错误！";
                }
            }

            return strErr;
        }
    }
}